#pragma once

#include "LayoutData.h"
#include "SkinnedButton.h"
#include "SkinnedText.h"
#include "resourceppc.h"

#define MAIN_WND_CLASS      TEXT("iConfig")
#define LAYOUT_FILE_NAME    TEXT("iConfig.dat")

class CMainFrame :
  public CFrameWindowImpl<CMainFrame>,
  public CMessageFilter,
  public CAppWindowBase<CMainFrame>
{
public:
  DECLARE_FRAME_WND_CLASS(MAIN_WND_CLASS, IDR_MAINFRAME)

  CMainFrame()
    : m_layoutFile(LAYOUT_FILE_NAME), m_activeList(NULL), m_hMenubar(NULL)
	{}

  virtual ~CMainFrame()
  {}

	virtual BOOL PreTranslateMessage(MSG* pMsg)
	{
		if(CFrameWindowImpl<CMainFrame>::PreTranslateMessage(pMsg))
			return TRUE;

    return FALSE;
  }

	BEGIN_MSG_MAP_EX(CMainFrame)
		MSG_WM_CREATE(OnCreate)
    MSG_WM_DESTROY(OnDestroy)

    MESSAGE_HANDLER(WM_COPYDATA,      OnCopyData)

    MSG_WM_SIZE(OnSize)
    MESSAGE_HANDLER(WM_ACTIVATE, OnActivate)
    MESSAGE_HANDLER(WM_SETTINGCHANGE, OnSettingChange)

    MESSAGE_HANDLER(WM_GETMENUBAR, OnGetMenubar)

    MESSAGE_HANDLER(WM_CTLCOLORSTATIC, OnColorEdit)

    COMMAND_ID_HANDLER_EX(IDCANCEL, OnClose)
    COMMAND_ID_HANDLER_EX(IDC_BTN_TABS_PREV, OnTabsPrev)
    COMMAND_ID_HANDLER_EX(IDC_BTN_TABS, OnTabs)
    COMMAND_ID_HANDLER_EX(IDC_BTN_TABS_NEXT, OnTabsNext)
    COMMAND_ID_HANDLER_EX(IDOK, OnSave)

		CHAIN_MSG_MAP(CFrameWindowImpl<CMainFrame>)
	END_MSG_MAP()

	int  OnCreate(LPCREATESTRUCT lpCreateStruct);
  void OnDestroy();

  LRESULT OnCopyData(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);

  void OnSize(UINT nType, CSize size);
  LRESULT OnActivate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
  LRESULT OnSettingChange(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);

  LRESULT OnGetMenubar(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  { return (LRESULT)m_hMenubar; }

  LRESULT OnColorEdit(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  {
    if ((HWND)lParam == m_header.m_hWnd)
      return m_header.SendMessage(uMsg, wParam, lParam);
    return bHandled = FALSE;
  }

  void OnClose(UINT uNotifyCode, int nID, CWindow wndCtl);
  void OnTabsPrev(UINT uNotifyCode, int nID, CWindow wndCtl);
  void OnTabs(UINT uNotifyCode, int nID, CWindow wndCtl);
  void OnTabsNext(UINT uNotifyCode, int nID, CWindow wndCtl);
  void OnSave(UINT uNotifyCode, int nID, CWindow wndCtl);

  void Close();
  void TabsPrev();
  void Tabs();
  void TabsNext();
  void Save();

  void UpdateWindowPosition(bool redraw = true);

protected:
  CString      m_layoutFile;
  CLayoutData  m_layout;
  CLayoutList* m_activeList;

  CSkinnedText   m_header;
  CSkinnedButton m_btnClose;
  CSkinnedButton m_btnTabsPrev;
  CSkinnedButton m_btnTabs;
  CSkinnedButton m_btnTabsNext;
  CSkinnedButton m_btnSave;

  SHACTIVATEINFO m_sai;
  HWND           m_hMenubar;
};